<?php


function loaddata_karyawan($id, $sk=true){
	if(!$sk){
		return loaddata_karyawan_start($id);
	}
	
	return load_data_karyawan_json($id);
}

function loaddata_karyawan_start($id, $sk=true){

	if((int)$id == 0 )return false;
	
	$query = "SELECT * FROM karyawan a 
		INNER JOIN biodata b ON a.biodata_id=b.biodata_id 
		LEFT JOIN kekaryawanan c ON c.karyawan_id=a.karyawan_id"; 
	$query .= " WHERE a.karyawan_id = {$id}";
	$result = my_query($query );
	$datas = array();
	$datas = my_fetch_array($result);
	if(!$sk){
		$datas['jabatan_id']=isset($datas['jabatan_gaji_id']) ? (int)$datas['jabatan_gaji_id'] : 0 ; 	
	}
	
	$golongan_jabatan_id = isset($datas['golongan_jabatan_id'] ) ? $datas['golongan_jabatan_id'] : 0  ;
	$golongan_jabatans=my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',$golongan_jabatan_id);
	$datas['golongan_jabatan'] = $golongan_jabatans['kode_golongan_jabatan'] ;
	$datas['kelompok_evaluasi'] = $golongan_jabatans['kelompok'];	
	$datas['lembur'] = $golongan_jabatans['lembur'] ;
	
	$kelompok_shift_id = isset($datas['kelompok_shift_id'] ) ? $datas['kelompok_shift_id'] : 0  ;
	$waktu_kerja_karyawan = my_query("
		SELECT a.kelompok_shift_id,a.waktu_kerja_id,b.label,a.kelompok_shift_kode FROM a_kelompok_shift a 
		INNER JOIN a_waktu_kerja b ON a.waktu_kerja_id=b.waktu_kerja_id 
		WHERE a.kelompok_shift_id ={$kelompok_shift_id} LIMIT 1
	");
	$waktu_kerja = my_fetch_array($waktu_kerja_karyawan ); 
	$datas['kelompok_shift_id'] = $waktu_kerja['kelompok_shift_id'] ;
	$datas['waktu_kerja'] = $waktu_kerja['label'] ;
	$datas['waktu_kerja_id'] = $waktu_kerja['waktu_kerja_id'] ;
	$datas['kelompok'] = $waktu_kerja['kelompok_shift_kode'] ;

	if(empty($datas['gelar'])){
		$datas['nama_gelar'] = ucfirst($datas['nama']) ;
	}else{
		$datas['nama_gelar']=trim(ucfirst($datas['nama'])).", ".$datas['gelar'] ;
	}	

	$status_id=isset($datas['status_id']) ? $datas['status_id'] : 0; 
	$statuss=my_get_data_by_id('status_kekaryawanan','status_id',$status_id);
	$datas['status_karyawan']=$statuss['label'];
	$datas['status_kekaryawan_id'] =$status_id;

//	$datas['tanggal_berakhir_status'] =$ckres['tanggal_berakhir'];
	$kota_id=isset($datas['tempat_lahir']) ? $datas['tempat_lahir'] : 0; 
	$kotas=my_get_data_by_id('kota','kota_id',$kota_id);
	$datas['kota_id']=$kota_id;
	$datas['tempat_lahir']=$kotas['nama_kota'];
	return $datas;
}

//JSON DATA LOAD
function create_data_karyawan_json($id){
	$path = 'files/services/karyawan_'.$id.'.json';
	if(file_exists($path))unlink($path);
	 
	if (!$handle = fopen(	$path, 'a'	)) {
		 return false;
	}
	 
	$datas 	= loaddata_karyawan_start($id);
	$res 	= json_encode($datas  );

	if (fwrite($handle, $res) === FALSE) {
		return false;
	}
	fclose($handle);
 
	
	return $res;
}

function load_data_karyawan_json($id){
	$path = 'files/services/karyawan_'.$id.'.json'; 
	if(!file_exists($path))return loaddata_karyawan_start($id);
	$contents = (string) file_get_contents($path); 
	$datas = json_decode($contents ,true); 
	return  $datas ;
}


function json_clean_decode($json, $assoc = false, $depth = 512, $options = 0) { 

    // search and remove comments like /* */ and // 
    $json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t](//).*)#", '', $json); 

    if(version_compare(phpversion(), '5.4.0', '>=')) { 
        $json = json_decode($json, $assoc, $depth, $options); 
    } 
    elseif(version_compare(phpversion(), '5.3.0', '>=')) { 
        $json = json_decode($json, $assoc, $depth); 
    } 
    else { 
        $json = json_decode($json, $assoc); 
    } 

    return $json; 
} 
//JSON DATA LOAD


function get_karyawan_by_name($name){
list( $nik , $nama) = explode("/" ,$name ); 
$nik = trim($nik);
if(trim($nik) <> ''){
	$query = "SELECT karyawan_id FROM karyawan a 
	INNER JOIN biodata b ON a.biodata_id=b.biodata_id ";
	$query .= " WHERE a.nik = '{$nik}'";
}else{
	$nama = strtolower($nama);
	$query = "SELECT karyawan_id FROM karyawan a 
	INNER JOIN biodata b ON a.biodata_id=b.biodata_id ";
	$query .= " WHERE b.nama = '{$name}'";
}
	$result = my_query($query);
	if( $row = my_fetch_array( $result ) ){
		return loaddata_karyawan($row['karyawan_id']);
	}
	return false;
}

function loaddata_kesehatan_karyawan($id){
	if((int)$id == 0 )return false;
	
	$query = "SELECT * FROM riwayat_kesehatan_karyawan a
	INNER JOIN riwayat_kesehatan b ON a.riwayat_kesehatan_id=b.riwayat_kesehatan_id
	WHERE a.karyawan_id = {$id} ";
	 
	$result = my_query($query);
	$ty = array();
	while($tx = my_fetch_array($result)){
		$ty[] = $tx;
	}
	
	return $ty;
}

function loaddata_pendidikan_karyawan($id){
	if((int)$id == 0 )return false;
	
	$query = "SELECT * FROM riwayat_pendidikan_karyawan a
	INNER JOIN riwayat_pendidikan b ON a.riwayat_pendidikan_id=b.riwayat_pendidikan_id
	WHERE a.karyawan_id = {$id} ";
	 
	$result = my_query($query);
	$ty = array();
	while($tx = my_fetch_array($result)){
		$ty[] = $tx;
	}
	
	return $ty;
}

function loaddata_keluarga_karyawan($id){
	if((int)$id == 0 )return false;
	
	$query = "SELECT * FROM keluarga_karyawan a
	INNER JOIN biodata c ON c.biodata_id = a.biodata_id
	INNER JOIN hubungan_keluarga b ON a.hubungan_keluarga_id=b.hubungan_keluarga_id
	WHERE a.karyawan_id = {$id} ";
	 
	$result = my_query($query);
	$ty = array();
	while($tx = my_fetch_array($result)){
		$ty[] = $tx;
	}
	
	return $ty;

}

function loaddata_pekerjaan_karyawan($id){
	if((int)$id == 0 )return false;
	
	$query = "SELECT * FROM riwayat_kerja_karyawan a 
	INNER JOIN riwayat_kerja b ON a.riwayat_kerja_id=b.riwayat_kerja_id
	WHERE a.karyawan_id = {$id} ";
	 
	$result = my_query($query);
	$ty = array();
	while($tx = my_fetch_array($result)){
		$ty[] = $tx;
	}
	
	return $ty;

}

function loaddata_hobi_karyawan($id){
	if((int)$id == 0 )return false;
	
	$query = "SELECT * FROM hobi_karyawan a 
	INNER JOIN hobi b ON a.hobi_id=b.hobi_id
	INNER JOIN hobi_kelompok c ON c.hobi_kelompok_id=b.hobi_kelompok_id
	WHERE a.karyawan_id = {$id} ";
	 
	$result = my_query($query);
	$ty = array();
	while($tx = my_fetch_array($result)){
		$ty[] = $tx;
	}
	
	return $ty;
} 

function loaddata_acara_pelatihan($id){
	if((int)$id == 0 )return false;
	
	$query = "SELECT *,c.label AS judul FROM acara_pelatihan a
	INNER JOIN  penyelenggara_pelatihan b ON b.penyelenggara_id = a.penyelenggara_id
	INNER JOIN  judul_materi_pelatihan c ON c.judul_pelatihan_id = a.judul_pelatihan_id
	INNER JOIN  modul_materi_pelatihan d ON d.modul_materi_pelatihan_id = a.modul_materi_pelatihan_id
	WHERE a.acara_pelatihan_id ={$id} ";
	$result = my_query($query);
	return my_fetch_array($result);
}

function load_silabus_acara_pelatihan($id){

}

function loaddata_jabatan($id){
	if((int)$id == 0 )return false;
	$datas = array();
	$datas =my_get_data_by_id('jabatan','jabatan_id',$id); 
	
	$departemen = my_get_data_by_id('departemen','departemen_id',(int) $datas['departemen_id']);	
	$pbi = my_get_data_by_id('pos_biaya','pos_biaya_id',(int) $datas['pos_biaya_id']);	
	$datas['departemen'] = $departemen['nama_departemen'] ;	
	$datas['pos_biaya'] = $pbi['code'] ;	
	return $datas;
}


function prokaryawan($karyawan_id){
	$karyawan = loaddata_karyawan($karyawan_id);
	$view  = form_header( "detail pegawai" , "cp"  );
	$view .= form_field_display( '<span class="label_form">'.$karyawan['nama_gelar'].'</span>'   , "<b>Nama karyawan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.strtoupper($karyawan['nik']).'</span>'   , "<b>NIK</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$karyawan['status_karyawan'].'</span>', "<b>Status karyawan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$karyawan['kelamin'].'</span>', "<b>Jenis Kelamin</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$karyawan['tempat_lahir'] .', '.   date('d M Y',strtotime($karyawan['tanggal_lahir'].' 00:00:01')).'</span>' , "<b>Tempat, tanggal lahir</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$karyawan['marital'].'</span>', "<b>Status Perkawinan </b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$karyawan['direktorat'].' <b>/</b> '.$karyawan['jabatan'] .'</span>'  , "<b>Direktorat / Jabatan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$karyawan['golongan_jabatan'].'/'.$karyawan['grade_id'].'</span>', "<b>Gol/Grade</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.date('d M Y',strtotime($karyawan['tanggal_diterima'] )  ) .'</span>'  , "<b>T M B</b>"    ); 
	$view .= form_footer( );

	return $view;
}				
